using System;
using System.Collections.Generic;
using System.Text;

using CMS.Data;
using CMS.Core.Domain;
using CMS.Core.DataInterfaces;
using CMS.Presenters.Views;
using AjaxPro;
using CMS.Core.DataInterfaces;
using CMS.Presenters.Views;

namespace CMS.Presenters.Presenters
{
    public class ListVanBanPresenter
    {
        private List<VanBan> objList;
        private IListVanBanView iObjView;
        private IVanBanDao iObjDAO;
        
        /// <summary>
        /// Raised when the user wants to cancel editing a object.  The containing ASPX page should 
        /// listen for this event.
        /// </summary>
        public EventHandler CancelUpdateEvent;

        /// <summary>
        /// Raised after the object has been successfully committed to the database. 
        /// </summary>
        public EventHandler UpdateCompleteEvent;

        //Attaches this presenter to the view's events.
        public ListVanBanPresenter(IListVanBanView iObjView, IVanBanDao iObjDAO)
        {
            if (iObjView == null) throw new ArgumentNullException("View may not be null");
            if (iObjDAO == null) throw new ArgumentNullException("LinhVucDao may not be null");
            this.iObjView = iObjView;
            this.iObjDAO = iObjDAO;

            SubscribeViewToEvents();
        }

        public ListVanBanPresenter() { 
        
        }


        void SubscribeViewToEvents()
        {
            iObjView.Load += OnViewLoad;
        }

        void OnViewLoad(object sender, EventArgs e)
        {
             //LoadViewFromModel();
        }

        public void LoadViewFromModel(string maNSD)
        {
            this.objList = this.iObjDAO.GetAllOrderMoreFields(new string[] { "TenVanBan" }, true);
            iObjView.VanBanList = objList;
            // iObjView.IsRole = NHibernateDaoFactory.Instance.GetKiemTraQuyenDao().CheckQuyenCapNhatThongTinDanhMuc(maNSD);
        }

        public void GetAllVanBan(string typeVB, string maNSD)
        {
            if (typeVB.Equals("ALL"))
            {
                this.LoadViewFromModel(maNSD);
            }
            else
            {
                if (typeVB.Equals("VBAD"))
                {
                    this.objList = this.iObjDAO.GetFieldEqualCriteria("IsVBAD", true);
                }

                if (typeVB.Equals("VBCD"))
                {
                    this.objList = this.iObjDAO.GetFieldEqualCriteria("IsVBCD", true);
                }

                if (typeVB.Equals("VBPL"))
                {
                    this.objList = this.iObjDAO.GetFieldEqualCriteria("IsVBPL", true);
                }
                iObjView.VanBanList = objList;

                // iObjView.IsRole = NHibernateDaoFactory.Instance.GetKiemTraQuyenDao().CheckQuyenCapNhatThongTinDanhMuc(maNSD);
            }
        }

        [AjaxMethod]
        public bool CheckUsedVanBan(int maVanBan)
        {
            VanBan vanban = new VanBanDao().GetById(maVanBan);
            if (new DonThuVBCDDao().GetDonThuWithVanBan(maVanBan).Count > 0)
            {
                return true;
            }
            return false;
        }

        #region Button Members
        // For Cancel button
        public void CancelUpdate()
        {
            CancelUpdateEvent(this, null);
        }

        // For Delete button
        public void ObjToDelete(int maVanBan)
        {
            this.iObjDAO.Delete(iObjDAO.GetById(maVanBan, false));            
        }

        // For Update button
        public void ObjToUpdate(int maVanBan)
        {
            VanBan obj = iObjDAO.GetById(maVanBan, false);            
            this.iObjDAO.SaveOrUpdate(obj);
        }
        #endregion
    }
}
